<?php

if(!defined("INCLUDE")) die("Nedostatečné oprávnění pro spouštění souboru.");

# --------------------------------------------------------------------------------------------------------------------------
# CLASS MAPPER
# --------------------------------------------------------------------------------------------------------------------------
# Mapper slouží k zpracování požadovaných akcí na SQL, využívá třídu SQL query, pomocí které sestavuje SQL dotazy na
# databázi. 
#
# --------------------------------------------------------------------------------------------------------------------------
# INFORMACE O SOUBORU
# --------------------------------------------------------------------------------------------------------------------------
# Autor: Zechy
# Poslední editace: 1. 12. 2012
#

class mapper
{
	private $sql;
	private $settings;

	public function __construct()
	{
		$this->sql = new sqlquery;
		$this->settings = new settings;
	}

	public function articles_count()
	{
		$this->sql->set_table("cms_articles");
		$this->sql->set_columns("COUNT(*) 'count'");
		$result = $this->sql->select();

		if($result)
		{
			$row = mysql_fetch_array($result);
			return $row["count"];
		}
		else
		{
			return false;
		}
	}

	public function getall_articles($paginate = true, $page)
	{
		$this->sql->set_table("cms_articles a, cms_categories c, cms_users u");
		$this->sql->set_columns("
			a.id 'id', a.heading 'heading', a.perex 'perex', a.content 'content', a.tags 'tags', a.datetime 'datetime', a.status 'status', a.readed 'readed',
			c.name 'category',
			u.name 'autor'
			");
		$this->sql->set_where("a.autor = u.id AND a.category = c.id AND a.deleted = 0");
		$this->sql->set_orderby("a.id DESC");

		if($paginate == true)
		{
			$onPage = $this->settings->get_setting("nastaveni_clanku", "kolik_na_stranku");
			$this->sql->set_limit($onPage, ($onPage * ($page - 1)));
		}

		$result = $this->sql->select();

		if($result)
		{
			return $result;
		}
		else
		{
			return false;
		}
	}

	public function get_article_by_id($id)
	{
		$this->sql->set_table("cms_articles");
		$this->sql->set_columns("autor, category, heading, perex, content, tags, datetime");
		$this->sql->set_where("id = $id");
		$result = $this->sql->select();

		if($result)
		{
			return $result;
		}
		else
		{
			return false;
		}
	}

	public function add_article(entity $entity)
	{
		$action_loger = new action_loger("cms_articles");

		$this->sql->set_table("cms_articles");
		$this->sql->set_columns("autor, category, heading, perex, content, datetime, tags, status");
		$this->sql->set_values(
			"'".$entity->get_autor()."', '".$entity->get_category()."', '".$entity->get_heading()."', '".$entity->get_content("perex")."', '".
			$entity->get_content("content")."', '".$entity->get_datetime()."', '".$entity->get_tags()."', '".$entity->get_status()."'");
		$result = $this->sql->insert();

		if($result)
		{
			if($action_loger->new_entry())
			{
				return 2;
			}
			else
			{
				return 1;
			}
		}
		else
		{
			return 0;
		}
	}

	public function edit_article(entity $entity)
	{
		$action_loger = new action_loger("cms_articles");

		if($action_loger->edit($entity->get_id()))
		{
			if($action_loger->save_previous_data( $entity->get_id() ) )
			{
				$this->sql->set_table("cms_articles");
				$this->sql->set_columns("category, heading, perex, content, tags, datetime, status");
				$edit = array(
					$entity->get_category(),
					$entity->get_heading(),
					$entity->get_content("perex"),
					$entity->get_content("content"),
					$entity->get_tags(),
					$entity->get_datetime(),
					1
				);
				$this->sql->set_values_array($edit);
				$this->sql->set_where("id = ".$entity->get_id());
				$result = $this->sql->update();

				if($result)
				{
					return 3;
				}
				else
				{
					$action_loger->delete_last_action();
					return 2;
				}
			}
			else
			{
				$action_loger->delete_last_action();
				return 1;
			}
		}
		else
		{
			return 0;
		}
	}

	public function delete_article($id)
	{
		$action_loger = new action_loger("cms_articles");

		$this->sql->setnull();
		$this->sql->set_table("cms_articles");
		$this->sql->set_columns("deleted");
		$this->sql->set_values_array(array("1"));
		$this->sql->set_where("id = $id");
		$result = $this->sql->update();

		if($result)
		{
			if($action_loger->delete($id))
			{
				return 2;
			}
			else
			{
				return 1;
			}
		}
		else
		{
			return 0;
		}
	}

	public function getall_categories()
	{
		$this->sql->set_table("cms_categories");
		$this->sql->set_columns("*");
		$this->sql->set_where("deleted = 0");
		$this->sql->set_orderby("id DESC");
		$result = $this->sql->select();

		if($result)
		{
			return $result;
		}
		else
		{
			return false;
		}
	}

	public function get_category_by_id($id)
	{
		$this->sql->set_table("cms_categories");
		$this->sql->set_columns("*");
		$this->sql->set_where("id = $id");
		$result = $this->sql->select();

		if($result)
		{
			return $result;
		}
		else
		{
			return false;
		}
	}

	public function new_category()
	{
		$action_loger = new action_loger("cms_categories");

		$this->sql->create_connect();
		$name = mysql_real_escape_string($_POST["name"]);
		$note = mysql_real_escape_string($_POST["note"]);
		$this->sql->close_connect();

		$this->sql->set_table("cms_categories");
		$this->sql->set_columns("name, note");
		$this->sql->set_values("'$name', '$note'");
		$result = $this->sql->insert();

		if($action_loger->new_entry())
		{
			if($result)
			{
				return 2;
			}
			else
			{
				return 1;
			}
		}
		else
		{
			return 0;
		}
	}

	public function edit_category($name, $note, $id)
	{
		$action_loger = new action_loger("cms_categories");

		$this->sql->create_connect();
		$name = mysql_real_escape_string($name);
		$note = mysql_real_escape_string($note);
		$values = array($name, $note);
		$this->sql->close_connect();

		if($action_loger->edit($id))
		{
			if($action_loger->save_previous_data($id))
			{
				$this->sql->set_table("cms_categories");
				$this->sql->set_columns("name, note");
				$this->sql->set_values_array($values);
				$this->sql->set_where("id = $id");
				$result = $this->sql->update();
				if($result)
				{
					return 3;
				}
				else
				{
					$action_loger->delete_last_action();
					return 2;
				}
			}
			else
			{
				$action_loger->delete_last_action();
				return 1;
			}
		}
		else
		{
			return 0;
		}
	}

	public function delete_category($id)
	{
		$action_loger = new action_loger("cms_categories");
		$this->sql->setnull();
		$this->sql->set_table("cms_articles");
		$this->sql->set_columns("category");
		$this->sql->set_values_array(array("1"));
		$this->sql->set_where("category = $id");
		$result = $this->sql->update();

		if($result)
		{
			$this->sql->setnull();
			$this->sql->set_table("cms_categories");
			$this->sql->set_columns("deleted");
			$this->sql->set_values_array(array("1"));
			$this->sql->set_where("id = $id");
			$result = $this->sql->update();

			if($result)
			{
				if($action_loger->delete($id))
				{
					return 3;
				}
				else
				{
					return 2;
				}
			}
			else
			{
				return 1;
			}
		}
		else
		{
			return 0;
		}
	}
}

?>